from pandas import DataFrame
from pandas import concat
from pandas import read_csv
from sklearn.preprocessing import MinMaxScaler
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import OneHotEncoder
def series_to_supervised(data,n_in=1,n_out=1,dropnan=True):
    n_var=1 if type(data) is list else data.shape[1]
    df=DataFrame(data)
    cols,names=list(),list()
    #输入序列
    for i in range(n_in,0,-1):
        cols.append(df.shift(i))
        names+=[('var%d(t-%d)'%(j+1,i))for j in range(n_var)]

    #预测序列

    for i in range(0,n_out):
        cols.append(df.shift(-i))
        if i==0:
            names+=[('var%d(t)'%(j+1))for j in range(n_var)]
        else:
            names+=[('var%d(t+%d)'%(j+1,i))for j in range(n_var)]

    #put it all together
    agg=concat(cols,axis=1)
    agg.columns=names
    #drop rows with Nan values
    if dropnan:
        agg.dropna(inplace=True)
    return agg
#load dataset
def Normalization(path,in_len=1,out_len=1):
    dataset_big=read_csv(path,header=0,index_col=0)
    values=dataset_big.values
    #integer encode weather and wind_direction
    encoder=LabelEncoder()
    length=values[:,2:].shape[1]
    for i in range(length):
        values[:,i+2]=encoder.fit_transform(values[:,i+2])
    # values[:,2]=encoder.fit_transform(values[:,2])
    # values[:,5]=encoder.fit_transform(values[:,5])
    # values[:,6]=encoder.fit_transform(values[:,6])
    values=values.astype('float32')
    #normalize features
    scaler=MinMaxScaler(feature_range=(0,1))
    scaled=scaler.fit_transform(values)
    reframe=series_to_supervised(scaled,in_len,out_len)
    reframe.drop(reframe.columns[9:],axis=1,inplace=True)
    return reframe,scaler
